﻿
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gstdio.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string.h>

#include "main.h"
#include "gbilling.h"
#include "gui.h"
#include "callback.h"
#include "server.h"
#include "sqldb.h"
#include "mongoose/mongoose.h"
#include "webserver.h"

// This function will be called by mongoose on every new request.
static int begin_request_handler(struct mg_connection *conn) {
  const struct mg_request_info *request_info = mg_get_request_info(conn);
  char content[9999];
  char tmp[9999];

GList *ptr= NULL;
  ClientData *data = NULL;
  GbillingPrepaid *prepaid= NULL;
  gchar *type= NULL,*state= NULL;
  char *path_info= NULL;
  static const gchar *status[] =
    {
        "Aktif",
        "Pindah",
        "Login",
        "Logout",
        "Nonaktif"
    };
      // Prepare the message we're going to send
  int content_length;
  //sound_play (GBILLING_EVENT_CHAT_RECEIVE);

  //if( conn->path_info )
  //path_info = &conn->path_info;
  sprintf(tmp,"<html><link rel=\"stylesheet\" type=\"text/css\" href=\"/www/main.css\"><script src=\"/www/jquery-1.9.1.js\"></script><script src=\"/www/main.js\"></script><title></title><div>URI : %s </div>", request_info->uri);
  strcpy(content,tmp);
  if(strcmp(request_info->uri,"/")!=0){
//    process_new_connection(conn);
    sprintf(tmp,".%s",request_info->uri);
    mg_send_file(conn,tmp);
    return 1;
  }
  /*sprintf(tmp,"<span class=config>DocumentRoot : %s"
              "<span class=config>PathInfo : %s"
              "<span class=config>Request : %s"  );
  */
  strcat(content,"<table border=\"1\" cellpadding=\"4\" colspacing=\"4\"><tr><th>Client</th><th>Ping</th><th>Access</th><th>Status</th><th>Username</th><th>Tipe</th><th>Mulai</th><th>Durasi</th><th>Selesai</th><th>Tarif</th></tr>");

  for (ptr = client_list; ptr; ptr = g_list_next (ptr))
  {

      data = ptr->data;
      switch (data->type)
        {
            case GBILLING_LOGIN_TYPE_PREPAID:
                prepaid = g_list_nth_data (prepaid_list, data->idtype);
                type = (gchar *) prepaid->name;
                break;
            default:
                type = (gchar *) gbilling_login_mode[data->type];
        }
        if (data->active && data->status == GBILLING_STATUS_NONE)
            state = status[0];
        else if (data->active && data->status == GBILLING_STATUS_MOVE)
            state = status[1];
        else if (data->active && data->status == GBILLING_STATUS_LOGIN)
            state = status[2];
        else if (data->active && data->status == GBILLING_STATUS_LOGOUT)
            state = status[3];
        else
            state = status[4];
        //                  1          2         3        4         5          6          7          8         9          10
      sprintf(tmp,

              "<tr class=\"%s\"> \
                <td>%s</td> \
                <td class=\"ping\">%i</td> \
                <td class=\"akses\">%i</td> \
                <td class=\"dn\">%s</td> \
                <td class=\"dn\">%s</td> \
                <td class=\"dn\">%s</td> \
                <td class=\"dn\">%s</td> \
                <td class=\"dn\">%s</td> \
                <td class=\"dn\">%s</td> \
                <td class=\"dn\">%s</td> \
                </tr>",
              state,
              data->name,
              data->pingact ,
              data->access ,
              state,
              data->username,
              type,
              time_t_to_string (data->tstart),
              time_to_string (data->duration),
              time_t_to_string (data->tend),
              cost_to_string (data->ucost + data->acost));
    /*
    start = time_t_to_string (cdata->tstart);
    dur = time_to_string (cdata->duration);
    end = time_t_to_string (cdata->tend);
    cost = cost_to_string (cdata->ucost + cdata->acost);
    */
      strcat(content,tmp);

  }
  strcat(content,"</table>");


  content_length= snprintf(tmp, sizeof(tmp),
                                "%s",
                                content);
  // Send HTTP reply to the client
  mg_printf(conn,
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Content-Length: %d\r\n"        // Always set Content-Length
            "\r\n"
            "%s",
            content_length, content);

  // Returning non-zero tells mongoose that our function has replied to
  // the client, and mongoose should not send client any more data.


  return 1;
}

int start_webserver(void) {
  struct mg_context *ctx;
  struct mg_callbacks callbacks;

  // List of options. Last element must be NULL.
  const char *options[] = {"listening_ports", "8080", NULL};


  // Prepare callbacks structure. We have only one callback, the rest are NULL.
  memset(&callbacks, 0, sizeof(callbacks));
  callbacks.begin_request = begin_request_handler;

  // Start the web server.
  ctx = mg_start(&callbacks, NULL, options);

  // Wait until user hits "enter". Server is running in separate thread.
  // Navigating to http://localhost:8080 will invoke begin_request_handler().
  getchar();

  // Stop the server.
  mg_stop(ctx);

  return 0;
}
